<?php
/**
 * CakeCMS - 品牌分类
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class BrandController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS品牌管理' );
	}
	public function index() {
		$this->set ( 'title_for_layout', 'CakeCMS品牌管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'brand.js' 
		) );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$this->loadModel ( "BrandCate" );
		
		$conditions = array (
				'parentid <=' => 15 //数据太多。只列出二级分类数据
		);
		$result = $this->BrandCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['BrandCate'] ['id'];
			$r ['name'] = $val ['BrandCate'] ['name'];
			$r ['img'] = $val ['BrandCate'] ['img'];
			$r ['parentid'] = $val ['BrandCate'] ['parentid'];
			$r ['tags'] = $val ['BrandCate'] ['tags'];
			$r ['is_hots'] = $val ['BrandCate'] ['is_hots'];
			$r ['status'] = $val ['BrandCate'] ['status'];
			$r ['listorder'] = $val ['BrandCate'] ['listorder'];
			$r ['color'] = $val ['BrandCate'] ['color'];
			if($val ['BrandCate'] ['id'] <= 100){
				$r ['str_manage'] = '<a href="javascript:;"><font color="#cccccc">添加子分类</font></a> | ';
				$r ['str_manage'] .= '<a href="javascript:;"><font color="#cccccc">修改</font></a> | ';
				$r ['str_manage'] .= '<a href="javascript:;"><font color="#cccccc">删除</font></a>';
			}else{
				//$r ['str_manage'] = '<a href="/Brand/add/' . $val ['BrandCate'] ['id'] . '/">添加子分类</a> | ';
				$r ['str_manage'] = '<a href="javascript:;"><font color="#cccccc">添加子分类</font></a> | ';
				$r ['str_manage'] .= '<a href="/Brand/edit/' . $val ['BrandCate'] ['id'] . '/">修改</a> | ';
				$r ['str_manage'] .= '<a href="javascript:;"><font color="#cccccc">删除</font></a>';
				//$r ['str_manage'] .= '<a href="javascript:;" onclick="del(' . $val ['BrandCate'] ['id'] . ')">删除</a>';
			}
			$array [] = $r;
		}
		
		$str = "<tr>
		<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
		<td align='center'>\$id</td>
		<td >\$spacer\$name</td>
		<td align='center'>\$str_manage</td>
		</tr>";
		
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		$this->set ( 'categorys', $select_categorys );
	}
	
	/**
	 * 添加分类
	 */
	public function add() {
		if ($this->request->isPost ()) {
			$data ['BrandCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['BrandCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['BrandCate'] ['img'] = $this->data ['img'];
			$data ['BrandCate'] ['parentid'] = $this->data ['parentid'];
			if (! isset ( $data ['BrandCate'] ['parentid'] )) {
				$this->flash ( '上级分类不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['BrandCate'] ['tags'] = $this->data ['tags'];
			$data ['BrandCate'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['BrandCate'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['BrandCate'] ['color'] = $this->data ['color'];
			
			$this->loadModel ( 'BrandCate' );
			if ($this->BrandCate->add ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( "添加品牌分类\"{$data ['BrandCate'] ['name']}\"" );
				
				$this->flash ( '添加品牌分类成功!', '/Brand/', 1250 );
				return false;
			} else {
				$this->flash ( '添加品牌分类失败!', '/Brand/add/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '添加品牌分类' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'brand.js',
					'colorpicker.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "BrandCate" );
			
			$parentid = $this->params ['parentid'];
			
			$conditions = array ('parentid'=>0);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['BrandCate'] ['id'];
				$r ['name'] = $val ['BrandCate'] ['name'];
				$r ['parentid'] = $val ['BrandCate'] ['parentid'];
				$r ['img'] = $val ['BrandCate'] ['img'];
				$r ['tags'] = $val ['BrandCate'] ['tags'];
				$r ['is_hots'] = $val ['BrandCate'] ['is_hots'];
				$r ['status'] = $val ['BrandCate'] ['status'];
				$r ['color'] = $val ['BrandCate'] ['color'];
				$r ['selected'] = $val ['BrandCate'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'select_categorys', $select_categorys );
		}
	}
	
	/**
	 * 编辑分类
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$data ['BrandCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['BrandCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['BrandCate'] ['img'] = $this->data ['img'];
			$data ['BrandCate'] ['parentid'] = $this->data ['parentid'];
			if (! isset ( $data ['BrandCate'] ['parentid'] )) {
				$this->flash ( '上级分类不能为空!', '/Goods/addCate/', 1250 );
				return false;
			}
			$data ['BrandCate'] ['tags'] = $this->data ['tags'];
			$data ['BrandCate'] ['is_hots'] = $this->data ['is_hots'] == 1 ? 1 : 0;
			$data ['BrandCate'] ['status'] = $this->data ['status'] == 1 ? 1 : 0;
			$data ['BrandCate'] ['color'] = $this->data ['color'];
			
			$data ['BrandCate'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( 'BrandCate' );
			if ($this->BrandCate->edit ( $data )) {
				
				// 记录操作日志
				$this->AdminLog->addLog ( "编辑品牌分类\"{$data ['BrandCate'] ['name']}\"" );
				
				$this->flash ( '编辑品牌分类成功!', '/Brand/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑品牌分类失败!', "/Brand/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '编辑添加品牌分类' );
			
			$id = $this->params ['id'];
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'brand.js',
					'colorpicker.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "BrandCate" );
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $id );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $parentid );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$conditions = array ('parentid'=>0);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['BrandCate'] ['id'];
				$r ['name'] = $val ['BrandCate'] ['name'];
				$r ['parentid'] = $val ['BrandCate'] ['parentid'];
				$r ['img'] = $val ['BrandCate'] ['img'];
				$r ['tags'] = $val ['BrandCate'] ['tags'];
				$r ['is_hots'] = $val ['BrandCate'] ['is_hots'];
				$r ['status'] = $val ['BrandCate'] ['status'];
				$r ['color'] = $val ['BrandCate'] ['color'];
				$r ['selected'] = $val ['BrandCate'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Brand_info = $this->BrandCate->GetBrandCateByID ( $id );
			$this->set ( 'Brand_info', $Brand_info );
			$this->set ( 'select_categorys', $select_categorys );
			$this->set ( 'id', $id );
			
			//基础分类
			$conditions = array ('parentid'=>$parentid);
			$result = $this->BrandCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC'
					)
			) );
			
			$parentid_Info = $this->BrandCate->GetBrandCateByID ( $id );
			$parentid = $parentid_Info ['BrandCate'] ['parentid'];
			
			$basic_str = "";
			foreach ( $result as $val ) {
				$selected = '';
				if($val ['BrandCate'] ['id']==$parentid){
					$selected = 'selected';
				}
				$basic_str .= "<option value='{$val ['BrandCate'] ['id']}' {$selected}>{$val ['BrandCate'] ['name']}</option>";
			}
			$this->set ( 'basic_categorys', $basic_str );
		}
	}
	/**
	 * 删除分类
	 */
	public function delete() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "BrandCate" );
		if ($this->BrandCate->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除品牌分类,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>